var ajaxUrl = require('../../../utils/url.js');
var encrypt = require('../../../utils/encrypt.js');
const app = getApp()
Page ({
  data: {
    from: [],
    to: [],
    currentTab: '0'
  },
  onLoad: function(){
    this.fetchData()
  },
  fetchData () {
    const self = this
    let params = {
			token: app.globalData.token
		}
		wx.request({
      method: "POST",
      url: ajaxUrl.getMessage(),
      data: params,
      success: function(res){
        if(res.data.code === 200){
          self.setData({
            from: res.data.data.from,
            to: res.data.data.to
          })
        }else{
          console.error(res.data.msg)
        }
      }
    })
  },
  bindChange: function( e ) {
    var self = this
    self.setData({ currentTab: e.detail.current })
  },
  swichNav: function( e ) {
    var self = this
    if( self.data.currentTab === e.target.dataset.current ) {
     return false
    } else {
     self.setData({
      currentTab: e.target.dataset.current
     })
    }
  },
  disagree (e) {
    let fromId = e.target.dataset.id
    const self = this
    let params = {
      action: 4,
      formId: fromId
    }
    params = encrypt.encrypt(params)
    wx.showModal({
      content: "确定执行拒绝操作?",
      confirmText: "确定",
			cancelText: "取消",
			success: function(res) {
				if (res.confirm) {
					wx.request({
						method: "POST",
						url: ajaxUrl.addMessage(),
						data: params,
						success: function(res){
							if(res.data.code === 200){
								wx.showToast({title:'操作成功'})
							}else{
								console.error(res.data.msg)
							}
						}
					})
				} else if (res.cancel) {
					console.log('用户点击取消')
				}
			}
    })
  },
  agree (e) {
    let fromId = e.target.dataset.id
    const self = this
    let params = {
      action: 3,
      formId: fromId
    }
    params = encrypt.encrypt(params)
    wx.showModal({
      content: "确定执行同意操作?",
      confirmText: "确定",
			cancelText: "取消",
			success: function(res) {
				if (res.confirm) {
					wx.request({
						method: "POST",
						url: ajaxUrl.addMessage(),
						data: params,
						success: function(res){
							if(res.data.code === 200){
								wx.showToast({title:'操作成功'})
							}else{
                console.error(res.data.msg)
							}
						}
					})
				} else if (res.cancel) {
					console.log('用户点击取消')
				}
			}
    })
  }
})